Skip to content

[Mono.Android] Compile RuntimeFeature into a single assembly to avoid conflict warnings#11669

Open
simonrozsival wants to merge 3 commits into
mainfrom
dev/simonrozsival/runtimefeature-single-assembly
Open

[Mono.Android] Compile RuntimeFeature into a single assembly to avoid conflict warnings#11669
simonrozsival wants to merge 3 commits into
mainfrom
dev/simonrozsival/runtimefeature-single-assembly

Conversation

@simonrozsival

Copy link
Copy Markdown
Member

Summary

Microsoft.Android.Runtime.RuntimeFeature was compiled into two assemblies:

  • Mono.Android.Runtime.dll (src/Mono.Android.Runtime/Mono.Android.Runtime.csproj)
  • Mono.Android.dll (src/Mono.Android/Mono.Android.csproj)

Because Mono.Android references Mono.Android.Runtime (which exposes its internals via InternalsVisibleTo), compiling Mono.Android.dll emitted 58 CS0436 warnings:

warning CS0436: The type 'RuntimeFeature' in
'src/Mono.Android/Microsoft.Android.Runtime/RuntimeFeature.cs' conflicts with the
imported type 'RuntimeFeature' in 'Mono.Android.Runtime'.

This PR makes RuntimeFeature live in exactly one assembly.

Changes

  • Mono.Android.csproj — remove the duplicate <Compile> of RuntimeFeature.cs, so it is compiled only into Mono.Android.Runtime.dll (the lower-level assembly that already owns it).
  • Mono.Android.Runtime/Properties/AssemblyInfo.cs.in — grant internals to Microsoft.Android.Runtime.NativeAOT (JreRuntime.cs) and Mono.Android.NET-Tests (test sources), which previously reached RuntimeFeature through Mono.Android.dll.
  • AndroidRuntime.cs, JNIEnv.cs, TypeManager.cs — add using RuntimeFeature = Microsoft.Android.Runtime.RuntimeFeature;. Once RuntimeFeature is only imported, the unqualified name becomes ambiguous with System.Runtime.CompilerServices.RuntimeFeature (CS0104). The alias disambiguates it, matching the alias already used in JNIEnvInit.cs.

Relationship to #11625

This is a focused subset of #11625, addressing only the RuntimeFeature duplication. It intentionally drops that PR's class rename (RuntimeFeatureAndroidRuntimeFeature) and the unrelated warning cleanups. (The CS0104 ambiguity above is exactly why #11625 renamed the type; here it is resolved with the using alias instead.)

Verification

Building Mono.Android + Microsoft.Android.Runtime.NativeAOT:

CS0436 CS0104 Build
Before 58 0
After 0 0

Metadata check (monodis --typedef) — Microsoft.Android.Runtime.RuntimeFeature is now defined in only one assembly:

Assembly Defines RuntimeFeature
Mono.Android.dll 0
Mono.Android.Runtime.dll 1
Microsoft.Android.Runtime.NativeAOT.dll 0

`Microsoft.Android.Runtime.RuntimeFeature` was compiled into both
`Mono.Android.Runtime.dll` and `Mono.Android.dll`. Because `Mono.Android`
references `Mono.Android.Runtime` (which exposes its internals to
`Mono.Android` via `InternalsVisibleTo`), building `Mono.Android.dll`
emitted 58 `CS0436` warnings:

    warning CS0436: The type 'RuntimeFeature' in
    'src/Mono.Android/Microsoft.Android.Runtime/RuntimeFeature.cs' conflicts
    with the imported type 'RuntimeFeature' in 'Mono.Android.Runtime'.

Compile `RuntimeFeature.cs` only into `Mono.Android.Runtime.dll` (the
lower-level assembly that already owns it) and let the other assemblies use
it through `InternalsVisibleTo`:

  * Remove the duplicate `<Compile Include="...RuntimeFeature.cs" />` from
    `Mono.Android.csproj`.
  * Grant `Mono.Android.Runtime` internals to
    `Microsoft.Android.Runtime.NativeAOT` and `Mono.Android.NET-Tests`, which
    previously accessed `RuntimeFeature` through `Mono.Android.dll`.
  * Once `RuntimeFeature` is only imported (no longer source-compiled) into
    `Mono.Android`, the unqualified name `RuntimeFeature` becomes ambiguous
    with `System.Runtime.CompilerServices.RuntimeFeature`. Add
    `using RuntimeFeature = Microsoft.Android.Runtime.RuntimeFeature;` to the
    three affected files, matching the alias already used in `JNIEnvInit.cs`.

`Microsoft.Android.Runtime.RuntimeFeature` is now defined in exactly one
assembly (`Mono.Android.Runtime.dll`), and the 58 `CS0436` warnings are gone.

This is a focused subset of #11625 that addresses only the `RuntimeFeature`
duplication, without that PR's class rename or unrelated warning cleanups.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 16, 2026 15:34
@simonrozsival simonrozsival changed the title [Mono.Android] Compile RuntimeFeature into a single assembly [Mono.Android] Compile RuntimeFeature into a single assembly to avoid conflict warnings Jun 16, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR removes the duplicate compilation of Microsoft.Android.Runtime.RuntimeFeature into Mono.Android.dll, ensuring it is defined only in Mono.Android.Runtime.dll to eliminate CS0436 type-conflict warnings and avoid CS0104 ambiguity with System.Runtime.CompilerServices.RuntimeFeature.

Changes:

  • Stop compiling Microsoft.Android.Runtime/RuntimeFeature.cs into Mono.Android.dll (leaving it only in Mono.Android.Runtime.dll).
  • Add InternalsVisibleTo entries in Mono.Android.Runtime for Microsoft.Android.Runtime.NativeAOT and Mono.Android.NET-Tests.
  • Disambiguate RuntimeFeature usage via using RuntimeFeature = Microsoft.Android.Runtime.RuntimeFeature; in the affected source files.
Show a summary per file
File Description
src/Mono.Android/Mono.Android.csproj Removes duplicate Compile entry for RuntimeFeature.cs to prevent cross-assembly type conflicts.
src/Mono.Android/Java.Interop/TypeManager.cs Adds RuntimeFeature type alias to avoid CS0104 ambiguity.
src/Mono.Android/Android.Runtime/JNIEnv.cs Adds RuntimeFeature type alias to avoid CS0104 ambiguity (while still using BCL RuntimeFeature fully-qualified).
src/Mono.Android/Android.Runtime/AndroidRuntime.cs Adds RuntimeFeature type alias to avoid CS0104 ambiguity.
src/Mono.Android.Runtime/Properties/AssemblyInfo.cs.in Grants internals access to NativeAOT and test assemblies now that RuntimeFeature is owned by Mono.Android.Runtime.

Copilot's findings

  • Files reviewed: 5/5 changed files
  • Comments generated: 0

@simonrozsival

Copy link
Copy Markdown
Member Author

@copilot we definitely changed something that affects the numbers of warnings and how our tests filter them. For example, this test is now failing:

[TESTLOG] Test BuildHasTrimmerWarningsMonoVMSuppressTrimAnalysisWarningsfalse[IL2055]True2 Starting
/Users/cloudtest/vss/_work/1/s/bin/Release/dotnet/dotnet build /Users/cloudtest/vss/_work/1/a/TestRelease/06-16_16.35.29/temp/BuildHasTrimmerWarningsMonoVMSuppressTrimAnalysisWarningsfalse[IL2055]True2/UnnamedProject.csproj /t:Build,SignAndroidPackage /noconsolelogger "/flp1:LogFile=/Users/cloudtest/vss/_work/1/a/TestRelease/06-16_16.35.29/temp/BuildHasTrimmerWarningsMonoVMSuppressTrimAnalysisWarningsfalse[IL2055]True2/build.log;Encoding=UTF-8;Verbosity=normal" -nodeReuse:false @"/Users/cloudtest/vss/_work/1/a/TestRelease/06-16_16.35.29/temp/BuildHasTrimmerWarningsMonoVMSuppressTrimAnalysisWarningsfalse[IL2055]True2/project.rsp"
Found Time Elapsed 00:00:32.9100000
[TESTLOG] Test BuildHasTrimmerWarningsMonoVMSuppressTrimAnalysisWarningsfalse[IL2055]True2 Complete
[TESTLOG] Test BuildHasTrimmerWarningsMonoVMSuppressTrimAnalysisWarningsfalse[IL2055]True2 Outcome=Failed

  Failed BuildHasTrimmerWarnings(MonoVM,"SuppressTrimAnalysisWarnings=false",["IL2055"],True,2) [33 s]
  Error Message:
     Should receive 2 warnings
  Expected: True
  But was:  False

  Stack Trace:
     at Xamarin.Android.Build.Tests.BuildTest2.BuildHasTrimmerWarnings(AndroidRuntime runtime, String properties, String[] codes, Boolean isRelease, Nullable`1 totalWarnings) in /Users/cloudtest/vss/_work/1/s/android/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest2.cs:line 573

1)    at Xamarin.Android.Build.Tests.BuildTest2.BuildHasTrimmerWarnings(AndroidRuntime runtime, String properties, String[] codes, Boolean isRelease, Nullable`1 totalWarnings) in /Users/cloudtest/vss/_work/1/s/android/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest2.cs:line 573


I want to
a) improve our build tests so that they have better assertions (not just "Expected: True But was: False" - not helpful)
b) make sure all tests still pass - read the full CI build logs and analyze them. make sure the assertions are updated after we reduced the number of warnings.

Copilot AI and others added 2 commits June 17, 2026 12:14
…adjust warning totals

Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
…HasTrimmerWarnings

Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>

Copilot AI commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

@copilot we definitely changed something that affects the numbers of warnings and how our tests filter them. For example, this test is now failing:

[TESTLOG] Test BuildHasTrimmerWarningsMonoVMSu...

Addressed in commits 7824101 and f54cbc8.

Changes made:

  • Improved BuildHasTrimmerWarnings assertions so failures now include the parsed warning summary line, build log path, and the warning lines that were found (instead of only Expected: True / But was: False).
  • Updated warning-count expectations for MonoVM/CoreCLR release cases where the total warning count dropped after RuntimeFeature deduplication.
  • Kept NativeAOT expected totals unchanged for the existing NativeAOT-specific warning behavior.

Validation:

  • Rebuilt Xamarin.Android.Build.Tests successfully with ./dotnet-local.sh build src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Xamarin.Android.Build.Tests.csproj.
  • Ran security scan + final review checks on the updated test changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants